﻿using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Synergy
{
    public partial class Form报工记录 : Form
    {
        public Form报工记录()
        {
            InitializeComponent();
        }

        private void button刷新_Click(object sender, EventArgs e)
        {
            查询();
        }

        private void 查询()
        {
            //string sql = $@"SELECT Z_baogong.id,生产订单号,生产订单行号,物料编号,Inventory.cInvName 物料名称,Inventory.cInvStd 物料规格,
            //                订单数量,工序名称, 报工数量,
            //                [Z_报工不良原因].数量 不良数量,[Z_报工不良原因].不良原因名称
            //                  FROM [Z_baogong] left join [Z_报工不良原因]
            //                  on Z_baogong.id = [Z_报工不良原因].报工id
            //                  left join Inventory on Inventory.cInvCode = [Z_baogong].物料编号";
            string sql = $@"SELECT ''序号,生产订单号,生产订单行号,物料编号,Inventory.cInvName 物料名称,Inventory.cInvStd 物料规格,
                        订单数量,Z_baogong.加工顺序,工序名称, CAST( 报工数量 as int) 报工数量,
                       CAST(   [不合格数量] as int) 不良数量,不良原因 不良原因名称
                        FROM [Z_baogong] 
                        left join Inventory on Inventory.cInvCode = [Z_baogong].物料编号 where 1=1";
            sql += " and CAST(   [不合格数量] as int) > 0";
            if (this.textBox1.Text != "")
            {
                sql += " and (z_baogong.物料编号 like '%" + this.textBox1.Text + "%' or Inventory.cinvname like '%" + this.textBox1.Text + "%' )";
            }
            if (this.comboBox工序.Text != "请选择")
            {
                sql += " and z_baogong.工序名称 = '" + this.comboBox工序.Text + "'";
            }
            if (this.textBox3.Text != "")
            {
                sql += " and z_baogong.生产订单号 like '%" + this.textBox3.Text + "%'";
            }

            if (this.dateTimePicker_ksrq.Checked)
            {
                sql += " and z_baogong.报工时间>='" + this.dateTimePicker_ksrq.Value.ToString("yyyy-MM-dd") + "'";
            }
            if (this.dateTimePicker_jsrq.Checked)
            {
                sql += " and z_baogong.报工时间<'" + this.dateTimePicker_jsrq.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
            }


            sql += @"      
                              order by 生产订单号,生产订单行号,Z_baogong.加工顺序";

            DataTable tb = pub.U8GetTable(sql);
            this.dataGridView1.AutoGenerateColumns = false;

            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            if (tb != null)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    tb.Rows[i]["序号"] = (i + 1).ToString();
                }

                textBox合计报工数.Text = tb.Compute("sum(报工数量)", "").ToString();
                textBox不良数.Text = tb.Compute("sum(不良数量)", "").ToString();
            }

            this.dataGridView1.DataSource = tb;
            this.dataGridView1.Refresh();
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            //如果 生产订单号	订单行号	物料编号	物料名称	物料规格	订单数量	工序名称	报工数量 数据一样，合并单元格
            // 对第1列相同单元格进行合并
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {
                using
                    (
                    Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor)
                    )
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        // 清除单元格
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                        // 画 Grid 边线（仅画单元格的底边线和右边线）
                        //   如果下一行和当前行的数据不同，则在当前的单元格画一条底边线
                        if (e.RowIndex < dataGridView1.Rows.Count - 1 &&
                        dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() !=
                        e.Value.ToString())
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                            e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom - 1);
                        // 画右边线
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                            e.CellBounds.Top, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom);

                        // 画（填写）单元格内容，相同的内容的单元格只填写第一个
                        if (e.Value != null)
                        {
                            if (e.RowIndex > 0 &&
                            dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() ==
                            e.Value.ToString())
                            { }
                            else
                            {
                                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                    Brushes.Black, e.CellBounds.X + 2,
                                    e.CellBounds.Y + 5, StringFormat.GenericDefault);
                            }
                        }
                        //e.Handled=true;这一句非常重要，必须加上，要不所画的内容就被后面的Painting事件刷新不见了！！！
                        e.Handled = true;
                    }
                }
            }
        }

        private void Form报工记录_Load(object sender, EventArgs e)
        {
            string sql = $@"select '请选择'  Description
                    union all
                    select Description from sfc_operation ";

            DataTable dt = SqlSugarHelp.scope.Ado.GetDataTable(sql);
            comboBox工序.DataSource = dt;
            comboBox工序.DisplayMember = "Description";
            comboBox工序.ValueMember = "Description";
            comboBox工序.SelectedIndex = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            saveFileDialog.FileName = $"报工记录{DateTime.Now.ToString("yyMMdd")}";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                MiniExcel.SaveAs(saveFileDialog.FileName, dataGridView1.DataSource, overwriteFile: true);
                MessageBox.Show("导出成功");
            }
        }
    }
}
